package com.yf1706.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yf1706.bean.BaseSalary;
import com.yf1706.bean.BaseSalarySob;
import com.yf1706.service.BaseSalaryService;
import com.yf1706.tools.JsonTool;
import com.yf1706.vo.BaseSalaryVO;

@Controller
@RequestMapping("baseSalary")
public class BaseSalaryController extends BaseController{
	
	@Autowired
	private BaseSalaryService bService;
	
	//--------------本薪账套------------------------
	@RequestMapping("querySobByPage")
	@ResponseBody
	public Map<String, Object> querySobByPage(int page,int rows,String sort,String order) {
		Page<BaseSalarySob> pages = bService.querySobByPage(page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<BaseSalarySob> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("findSobByPage")
	@ResponseBody
	public Map<String, Object> findSobByPage(String baseSalarySobName,int page,int rows,String sort,String order) {
		Page<BaseSalarySob> pages = bService.findSearch(baseSalarySobName, page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<BaseSalarySob> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	
	@RequestMapping("insertSob")
	@ResponseBody
	public void insertSob(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_bssob_rowData"));
		BaseSalarySob bSalarySob = new BaseSalarySob();
		bSalarySob.setBaseSalarySobName(jObject.getString("baseSalarySobName"));
		bSalarySob.setRemark(jObject.getString("remark"));
		bSalarySob.setBuilder(getSessionUserName(request));
		bSalarySob.setBuildDate(new Date());
		JsonTool jTool = new JsonTool();
		try {
			bService.saveSob(bSalarySob);
			jTool.setSuccess(true);
			jTool.setMsg("添加本薪账套【"+bSalarySob.getBaseSalarySobName()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加本薪账套【"+bSalarySob.getBaseSalarySobName()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("updateSob")
	@ResponseBody
	public void updateSob(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_bssob_rowData"));
		BaseSalarySob bSalarySob = new BaseSalarySob();
		bSalarySob.setBaseSalarySobId(jObject.getIntValue("baseSalarySobId"));
		bSalarySob.setBaseSalarySobName(jObject.getString("baseSalarySobName"));
		bSalarySob.setRemark(jObject.getString("remark"));
		JsonTool jTool = new JsonTool();
		try {
			bService.updateSob(bSalarySob);
			jTool.setSuccess(true);
			jTool.setMsg("编辑本薪账套【"+bSalarySob.getBaseSalarySobName()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑本薪账套【"+bSalarySob.getBaseSalarySobName()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("deleteSob")
	@ResponseBody
	public void deleteSob(HttpServletRequest request,HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_bssob_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			//bService.removeSob(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除本薪账套【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除本薪账套【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("queryAllSob")
	@ResponseBody
	public List<BaseSalarySob> queryAllSob(){
		return bService.querySobAll();
	}
	
	//--------------本薪------------------------
	
	@RequestMapping("findBSByPage")
	@ResponseBody
	public Map<String, Object> findBSbypage(int id,int page,int rows,String sort,String order) {
		Page<BaseSalaryVO> pages = bService.queryBSByPage(id, page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<BaseSalaryVO> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("insertBS")
	@ResponseBody
	public void insertBS(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("Data"));
		BaseSalary bSalary = new BaseSalary();
		bSalary.setBaseSalarySob(bService.findSobByID(jObject.getIntValue("baseSalarySobId")));
		bSalary.setEmp(bService.findEmpByID(jObject.getIntValue("empId")));
		bSalary.setBaseSalaryValue(jObject.getDoubleValue("baseSalaryValue"));
		bSalary.setSalaryBeginDate(jObject.getDate("salaryBeginDate"));
		bSalary.setMender(jObject.getString("mender"));
		bSalary.setMendDate(jObject.getDate("mendDate"));
		bSalary.setApprover(jObject.getString("approver"));
		bSalary.setApproveDate(jObject.getDate("approveDate"));
		bSalary.setRemark(jObject.getString("remark"));
		JsonTool jTool = new JsonTool();
		try {
			bService.saveBS(bSalary);
			jTool.setSuccess(true);
			jTool.setMsg("添加本薪成功！");
		} catch (Exception e) {
			jTool.setMsg("添加本薪失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("updateBS")
	@ResponseBody
	public void updateBS(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_bs_rowData"));
		BaseSalary baseSalary = new BaseSalary();
		baseSalary.setApproveDate(jObject.getDate("approveDate"));
		baseSalary.setApprover(jObject.getString("approver"));
		baseSalary.setBaseSalaryId(jObject.getIntValue("baseSalaryId"));
		baseSalary.setBaseSalarySob(bService.findSobByID(jObject.getIntValue("baseSalarySobId")));
		baseSalary.setBaseSalaryValue(jObject.getDoubleValue("baseSalaryValue"));
		baseSalary.setMendDate(jObject.getDate("mendDate"));
		baseSalary.setMender(jObject.getString("mender"));
		baseSalary.setRemark(jObject.getString("remark"));
		baseSalary.setSalaryBeginDate(jObject.getDate("salaryBeginDate"));
		baseSalary.setSalaryOffDate(jObject.getDate("salaryOffDate"));
		JsonTool jTool = new JsonTool();
		try {
			bService.updateBS(baseSalary);
			jTool.setSuccess(true);
			jTool.setMsg("编辑员工：【"+baseSalary.getEmp().getEmpName()+"】的本薪成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑员工：【"+baseSalary.getEmp().getEmpName()+"】的本薪失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
	@RequestMapping("deleteBS")
	@ResponseBody
	public void deleteBS(HttpServletRequest request,HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_bs_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			//bService.removeBS(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除员工本薪【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除员工本薪【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
	
}
